package com.atguigu.self.gulimall.ware.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.self.common.utils.R;
import com.atguigu.self.gulimall.ware.feign.ProductFeignService;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.self.common.utils.PageUtils;
import com.atguigu.self.common.utils.Query;

import com.atguigu.self.gulimall.ware.dao.WareSkuDao;
import com.atguigu.self.gulimall.ware.entity.WareSkuEntity;
import com.atguigu.self.gulimall.ware.service.WareSkuService;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private WareSkuDao wareSkuDao;
    @Autowired
    ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        // 条件查询
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if (StringUtils.isNotBlank(skuId)) {
            queryWrapper.eq("sku_id", skuId);

        }

        String wareId = (String) params.get("wareId");
        if (StringUtils.isNotBlank(wareId)) {
            queryWrapper.eq("ware_id", wareId);
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        List<WareSkuEntity> wareSkuEntities = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (wareSkuEntities == null || wareSkuEntities.size() == 0) {
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setStockLocked(0);
            // TODO 远程查询sku信息； 库存表设置sku name(商品名称), try-catch 发生异常并不会导致事物回滚， 异常处理掉了
            //  发生异常时使用 try-catch 可以使事物不回滚，还有别的方式使事物不回滚吗？ 高级篇讲到
            try {
                R skuInfo = productFeignService.info(skuId);
                if (skuInfo.getCode() == 0) {
                    // 获取sku对象数据
                    Map<String, Object> hashMap = (HashMap<String, Object>) skuInfo.get("skuInfo");
                    // 设置sku name
                    wareSkuEntity.setSkuName((String) hashMap.get("skuName"));
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }

            wareSkuDao.insert(wareSkuEntity);

        } else {
            wareSkuDao.addStock(skuId, wareId, skuNum);

        }
    }

}